/*
 * Copyright (C) 2013 Square, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import { Address } from "./Address";
import { SocketAddress } from "./Socket/SocketAddress";
import { Proxy, ProxyType } from "./Socket/Proxy"
/**
 * The concrete route used by a connection to reach an abstract origin server. When creating a
 * connection the client has many options:
 *
 *  * **HTTP proxy:** a proxy server may be explicitly configured for the client.
 * Otherwise the proxy selector is used. It may return multiple proxies to attempt.
 *  * **IP address:** whether connecting directly to an origin server or a proxy,
 * opening a socket requires an IP address. The DNS server may return multiple IP addresses
 * to attempt.
 *
 * Each route is a specific selection of these options.
 */
export class Route {
  /** The address of the origin server. */
  readonly address: Address

  /**
   * The proxy server used for this route.
   * **Warning:** This may disagree with [Address.proxy] when it is null. When
   * the address's proxy is null, the proxy selector is used.
   */
  readonly proxy: Proxy

  /** The specific socket address (IP and port) to connect to. */
  readonly socketAddress: SocketAddress

  /**
   * Creates a new Route.
   * @param address - The address of the origin server
   * @param proxy - The proxy server to use
   * @param socketAddress - The specific socket address to connect to
   */
  constructor(address: Address, proxy: Proxy, socketAddress: SocketAddress) {
    this.address = address
    this.proxy = proxy
    this.socketAddress = socketAddress
  }

  /**
   * Returns true if this route tunnels HTTPS through an HTTP proxy.
   * See [RFC 2817, Section 5.2][rfc_2817].
   *
   * [rfc_2817]: http://www.ietf.org/rfc/rfc2817.txt
   */
  requiresTunnel(): boolean {
    return this.address.sslSocketFactory !== null && this.proxy.proxyType === ProxyType.HTTP
  }

  /**
   * Compares this route with another object for equality.
   * @param other - The object to compare with
   * @returns true if the routes are equal, false otherwise
   */
  equals(other: unknown): boolean {
    return other instanceof Route &&
      other.address === this.address &&
      other.proxy === this.proxy &&
      other.socketAddress === this.socketAddress
  }

  /**
   * Returns a hash code for this route.
   * Note: This is a simple implementation. For production use with hash-based
   * collections, consider implementing proper hashCode methods for Address,
   * Proxy, and SocketAddress.
   * @returns A hash code value for this route
   */
  hashCode(): number {
    let result = 17
    // Simple hash combination - should be replaced with proper hashCode if needed
    result = 31 * result + (this.address ? this.address.toString().length : 0)
    result = 31 * result + (this.proxy ? this.proxy.toString().length : 0)
    result = 31 * result + (this.socketAddress ? this.socketAddress.toString().length : 0)
    return result
  }

  /**
   * Returns a string representation of this route.
   * @returns A string describing this route
   */
  toString(): string {
    return `Route ${this.socketAddress}`
  }
}